const _html = function (body) {
  let html = ''
  if (Util.isIos() && Util.isWeixn()) {
    html = body
  } else {
    html = `<!DOCTYPE html>
   <html lang="en">

   <head>
       <meta charset="UTF-8">
       <style>
           * {
               margin: 0;
               padding: 0;
           }

           body {
               min-width: 375px;
               font-size:14px;
           }

           img {
               max-width: 100%;
           }
       </style>
   </head>

   <body>
   ${body}
   </body>

   </html>`
  }
  return html
}

const _infoTab = {

  // 自营详情
  '1': function (product) {
    let goods_introduce = ''
    /* if (product.goods_introduce_mobile) {
      let goods_introduce_mobile = product.goods_introduce_mobile
      let mobile = JSON.parse(goods_introduce_mobile)
      mobile.map(function (item, index) {
        if (item.type == 'text') {
          goods_introduce += `<div>${item.value}</div>`
          return
        }
        if (item.type == 'image') {
          goods_introduce += `<div><img style="max-width:100%" src="${item.value}" /></div>`
        }
      })
    } else {
      goods_introduce = product.goods_introduce
    } */
    goods_introduce = product.goods_introduce
    let goods_specifications = ''
    if (product.goods_specifications_mobile) {
      let goods_specifications_mobile = product.goods_specifications_mobile
      let mobile = JSON.parse(goods_specifications_mobile)
      mobile.map(function (item, index) {
        if (item.type == 'text') {
          goods_specifications += `<div>${item.value}</div>`
          return
        }
        if (item.type == 'image') {
          goods_specifications += `<div><img style="max-width:100%" src="${item.value}" /></div>`
        }
      })
    } else {
      goods_specifications = product.goods_specifications
    }

    let goods_after_sale = ''
    if (product.goods_after_sale_mobile) {
      let goods_after_sale_mobile = product.goods_after_sale_mobile
      let mobile = JSON.parse(goods_after_sale_mobile)
      mobile.map(function (item, index) {
        if (item.type == 'text') {
          goods_after_sale += `<div>${item.value}</div>`
          return
        }
        if (item.type == 'image') {
          goods_after_sale += `<div><img style="max-width:100%" src="${item.value}" /></div>`
        }
      })
    } else {
      goods_after_sale = product.goods_after_sale
    }

    let tab = [
      {
        text: '商品详情',
        view: goods_introduce
      },
      {
        text: '产品参数',
        view: _html(Util.richtext(goods_specifications))
      },
      {
        text: '售后保障',
        view: _html(Util.richtext(goods_after_sale))
      }]

    return tab
  },
  // JD详情
  '-1': function (product) {
    let goods_introduce, tab
    let goods_specifications = product.param
    if (product.wxintroduction && product.wxintroduction.length) {
      goods_introduce = product.wxintroduction
      tab = [
        {
          text: '商品详情',
          view: goods_introduce
        },
        {
          text: '产品参数',
          view: _html(Util.richtext(goods_specifications))
        }]
    } else {
      goods_introduce = product.appintroduce || product.introduction
      tab = [
        {
          text: '商品详情',
          view: _html(Util.richtext(goods_introduce))
        },
        {
          text: '产品参数',
          view: _html(Util.richtext(goods_specifications))
        }]
    }
    return tab
  },
  // 苏宁详情
  '-4': function (product) {
    let goods_introduce, tab
    let goods_specifications = product.param
    if (product.wxintroduction && product.wxintroduction.length) {
      goods_introduce = product.wxintroduction
      tab = [
        {
          text: '商品详情',
          view: goods_introduce
        },
        {
          text: '产品参数',
          view: _html(Util.richtext(goods_specifications))
        }]
    } else {
      goods_introduce = product.appintroduce || product.introduction
      tab = [
        {
          text: '商品详情',
          view: _html(Util.richtext(goods_introduce))
        },
        {
          text: '产品参数',
          view: _html(Util.richtext(goods_specifications))
        }]
    }
    return tab
  },
  // 严选详情
  '-2': function (product) {
    let goods_introduce = product.detailHtml

    let tab = [
      {
        text: '商品详情',
        view: _html(Util.richtext(goods_introduce))
      }
    ]
    return tab
  }
}

export default class Util {
  static richtext (text) {
    let regex1 = new RegExp('(i?)(\<img)(?!(.*?style=[\'"](.*)[\'"])[^\>]+\>)', 'gmi')
    text = text ? text.replace(regex1, '$2 style=""$3') : text
    var regex2 = new RegExp('(i?)(\<img.*?style=[\'"])([^\>]+\>)', 'gmi')
    text = text ? text.replace(regex2, '$2display:block;max-width:100%;$3') : text
    return text
  }

  static isWeixn () {
    var ua = navigator.userAgent.toLowerCase()
    if (ua.match(/MicroMessenger/i) == 'micromessenger') {
      return ua.match(/wxwork/i) != 'wxwork'
    } else {
      return false
    }
  }

  static isUleeCommunityAPP () {
    var ua = navigator.userAgent.toLowerCase()
    if (ua.match(/UleeCommunityAPP/i) == 'uleecommunityapp') {
      return true
    } else {
      return false
    }
  }

  static isIos () {
    var ua = navigator.userAgent.toLowerCase()
    if (ua.match(/iPhone/i) == 'iphone') {
      return true
    } else {
      return false
    }
  }

  static isAndroid () {
    var ua = navigator.userAgent.toLowerCase()
    if (ua.match(/Android/i) == 'android') {
      return true
    } else {
      return false
    }
  }

  static setupWebViewJavascriptBridge (callback) {
    if (Util.isIos()) {
      if (window.WebViewJavascriptBridge) {
        // eslint-disable-next-line no-undef
        return callback(WebViewJavascriptBridge)
      }
      if (window.WVJBCallbacks) {
        return window.WVJBCallbacks.push(callback)
      }
      window.WVJBCallbacks = [callback] // 创建一个 WVJBCallbacks 全局属性数组，并将 callback 插入到数组中。
      var WVJBIframe = document.createElement('iframe') // 创建一个 iframe 元素
      WVJBIframe.style.display = 'none' // 不显示
      WVJBIframe.src = 'wvjbscheme://__BRIDGE_LOADED__' // 设置 iframe 的 src 属性
      document.documentElement.appendChild(WVJBIframe) // 把 iframe 添加到当前文导航上。
      setTimeout(function () {
        document.documentElement.removeChild(WVJBIframe)
      }, 0)
    }
    if (Util.isAndroid()) {
      if (window.WebViewJavascriptBridge) {
        // callback()
        // eslint-disable-next-line no-undef
        callback(WebViewJavascriptBridge)
      } else {
        // document.addEventListener(
        //   'WebViewJavascriptBridgeReady',
        //   // callback,
        //   function () {
        //     // eslint-disable-next-line no-undef
        //     callback(WebViewJavascriptBridge)
        //   },
        //   false
        // )
        document.addEventListener('WebViewJavascriptBridgeReady', function (event) {
          // eslint-disable-next-line no-undef
          if (window.onWebViewJavascriptBridgeReady) window.onWebViewJavascriptBridgeReady(window.__bridge = WebViewJavascriptBridge)
          // eslint-disable-next-line no-undef
          callback(WebViewJavascriptBridge)
        }, false)
      }
    }
  }

  static infoTab (product) {
    let source
    let tab = []
    if (product.supplier_id >= 1) {
      source = 1
    } else {
      source = product.supplier_id
    }
    if (_infoTab[source]) {
      tab = _infoTab[source](product)
    }
    return tab
  }

  /**
   * @desc 获取导航栏’？‘后面的参数
   */
  static UrlParser () {
    let query = {}
    let url = []
    if (window.location.search.slice(1)) {
      url = location.search.slice(1).split('&')
    } else if (window.location.hash.slice(1).split('?')[1]) {
      url = location.hash.slice(1).split('?')[1].split('&')
    } else {
      return undefined
    }
    url.forEach(function (item) {
      let items = item.split('=')
      query[items[0]] = items[1]
    })
    return query
  }
}
